#ifndef __APP_LIB_SOCKET_BASE_H__
#define __APP_LIB_SOCKET_BASE_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/un.h>
#include <netinet/in.h>

#define APP_LIB_SOCK_LISTEN_MAX            10

typedef enum __APP_LIB_SOCK_TYPE_e_
{
    APP_LIB_SOCK_TYPE_SRV = 0,
    APP_LIB_SOCK_TYPE_CLI,

    APP_LIB_SOCK_TYPE_MAX,
} APP_LIB_SOCK_TYPE_e;

typedef enum __APP_LIB_SOCKET_TYPE_e__
{
    APP_LIB_SOCKET_TYPE_UNIX_STREAM = 0,
    APP_LIB_SOCKET_TYPE_UNIX_DGRAM,
    APP_LIB_SOCKET_TYPE_INET_STREAM,
    APP_LIB_SOCKET_TYPE_INET_DGRAM,
    APP_LIB_SOCKET_TYPE_INET6_STREAM,
    APP_LIB_SOCKET_TYPE_INET6_DGRAM,

    APP_LIB_SOCKET_TYPE_MAX
} APP_LIB_SOCKET_TYPE_e;

typedef struct __APP_LIB_SOCKET_ADDR_s__
{
    union {
        struct sockaddr_un  stUnix;
        struct sockaddr_in  stIPv4;
        struct sockaddr_in6 stIPv6;
    };

    socklen_t       length;
} APP_LIB_SOCKET_ADDR_s;

typedef struct __APP_LIB_SOCK_s__
{
    union {
        SINT32            iSock;
        SINT32            aiSock[2];
    };

    APP_LIB_SOCKET_TYPE_e enType;
    APP_LIB_SOCKET_ADDR_s stAddr;
} APP_LIB_SOCK_s;

extern ULONG AppLibSock_InitUnixAddr(CHAR *pcUnixPath, INOUT APP_LIB_SOCKET_ADDR_s *pstAddr);
extern ULONG AppLibSock_InitInetAddr(IN APP_LIB_SOCKET_TYPE_e enType, CHAR *pcIpAddr, UINT16 usPort, INOUT APP_LIB_SOCKET_ADDR_s *pstAddr);
extern ULONG AppLibSock_Socket(INOUT APP_LIB_SOCK_s *pstSock);
extern ULONG AppLibSock_Bind(IN APP_LIB_SOCK_s *pstSock);
extern ULONG AppLibSock_Connect(INOUT APP_LIB_SOCK_s *pstCliSock);
extern ULONG AppLibSock_SrvCreate(IN APP_LIB_SOCK_s *pstSock, IN SINT32 iListen);
extern ULONG AppLibSock_Accept(IN APP_LIB_SOCK_s *pstSrv, INOUT APP_LIB_SOCK_s *pstCli);
extern ULONG AppLibSock_Recv(INOUT APP_LIB_SOCK_s *pstSock, void *data, SINT32 len, SINT32 flag);
extern ULONG AppLibSock_Send(INOUT APP_LIB_SOCK_s *pstSock, void *data, SINT32 len, SINT32 flag);
extern ULONG AppLibSock_SocketPair(INOUT APP_LIB_SOCK_s *pstSock);
extern ULONG AppLibSock_Close(INOUT APP_LIB_SOCK_s *pstSock);

#ifdef __cplusplus
}
#endif

#endif
